﻿#pragma once
#include<iostream>
#include<assert.h>
#include<vector>
#include<utility>
using namespace std;
template<class K,class V>
struct AVLTreeNode
{
	pair<K, V> _kv;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	int _bf;//平衡因子
	AVLTreeNode(const pair<K, V>kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}


};
template<typename K,typename V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	void InOrder()
	{
		_InOrder(_root);
	}
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}
	
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
				return cur;
		}
		return nullptr;
	}


	void RotateR(Node* parent)//右单旋
	{
		Node* parentp = parent->_parent;
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		if (subLR)
		{
			subLR->_parent = parent;
		}
		parent->_left = subLR;
		subL->_parent = parentp;
		subL->_right = parent;
		parent->_parent = subL;
		if (parentp == nullptr)
		{
			_root = subL;
			subL->_parent = parentp;
		}
		else
		{
			if (parentp->_left == parent)
			{
				parentp->_left = subL;
			}
			else
			{
				parentp->_right = subL;
			}
			subL->_parent = parentp;
		}
		parent->_bf = subL->_bf = 0;
	}


	void RotateL(Node* parent)//左单旋
	{
		Node* parentp = parent->_parent;
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		if (subRL)
		{
			subRL->_parent = parent;
		}
		parent->_right = subRL;

		subR->_parent = parentp;

		
		

		subR->_left = parent;
		parent->_parent = subR;
		parent->_bf = subR->_bf = 0;
		if (parentp == nullptr)
		{
			_root = subR;
		}
		else
		{
			if (parentp->_left == parent)
			{
				parentp->_left = subR;
			}
			else
			{
				parentp->_right = subR;
			}
		}
		
	}


	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;
		RotateR(parent->_right);
		RotateL(parent);
		if (bf == 0)
		{
			subR->_bf = subRL->_bf = parent->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = -1;
			subR->_bf = subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else
		{
			perror("RotateRL file");
		}

	}
	

	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;
		RotateL(parent->_left);
		RotateR(parent);
		if (bf == 0)
		{
			subL->_bf = subLR->_bf = parent->_bf = 0;
		}
		else if (bf == 1)
		{
			subL->_bf = -1;
			parent->_bf = subLR->_bf = 0;
			
		}
		else if (bf == -1)
		{
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else
		{
			perror("RotateLR file");
		}

	}
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		else
		{
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur != nullptr)
			{
				parent = cur;
				if (cur->_kv.first > kv.first)
				{
					cur = cur->_left;
				}
				else if(cur->_kv.first<kv.first)
				{
					cur = cur->_right;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(kv);
			if (kv.first < parent->_kv.first)
			{
				parent->_left = cur;
				//parent->_bf--;
			}
			else
			{
				parent->_right = cur;
				//parent->_bf++;

			}
			cur->_parent = parent;
			while (parent)
			{
				

				if (cur==parent->_left)
				{
					parent->_bf--;
				}
				else
				{
					parent->_bf++;
				}
				if (parent->_bf == 0)break;
				else if (parent->_bf == 1 || parent->_bf == -1)
				{
					//继续更新
					cur = parent;
					parent = parent->_parent;
				}
				else if (parent->_bf == 2 || parent->_bf == -2)
				{
					if (parent->_bf == -2 && cur->_bf == -1)
					{
						RotateR(parent);
					}
					else if (parent->_bf == 2 && cur->_bf == 1)
					{
						RotateL(parent);
					}
					else if (parent->_bf == -2 && cur->_bf == 1)
					{
						RotateLR(parent);
					}
					else if (parent->_bf == 2 && cur->_bf == -1)
					{
						RotateRL(parent);
					}
					else
					{
						assert(false);
					}
					
					break;
				}
				else
				{
					perror("insert false");
				}
				
			}
		}
		return  true;
	}
	int Height()
	{
		return _Height(_root);
	}
	int Size()
	{
		return _Size(_root);
	}
private:
	Node* _root=nullptr;
	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;

		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	//int _Height(Node* root)
	//{
	//	if (root == nullptr)
	//		return 0;
	//	int leftHeight = _Height(root->_left);
	//	int rightHeight = _Height(root->_right);
	//	return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	//}
	void _InOrder(Node* cur)
	{
		if (cur == nullptr)return;
		_InOrder(cur->_left);
		
		cout << cur->_kv.first<<":" << cur->_kv.second << endl;
		_InOrder(cur->_right);
	}
	bool _IsBalanceTree(Node* root)
	{


		if (nullptr == root)
			return true;


		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		}
		if (root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}
		// pRoot

		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}
};